package com.example.myapplication1;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class MyAdapter extends BaseAdapter{

    public  List<Msg> data;
    public Context mContext;
    public int resId;

    public MyAdapter(List<Msg> data, Context mContext, int resId) {
        this.data = data;
        this.mContext = mContext;
        this.resId = resId;
    }

    @Override
    public int getCount() {
        return data.size();
    }



    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View view1= LayoutInflater.from(mContext).inflate(resId,null);
        ImageView head=view1.findViewById(R.id.head1);
        TextView nickname=view1.findViewById(R.id.name);
        TextView content=view1.findViewById(R.id.msg);
        ImageView isLike=view1.findViewById(R.id.dianzan);

        Msg m =data.get(i);

        head.setImageResource(m.getProfile());
        nickname.setText(m.getNickname());
        content.setText(m.getContent());

        if(m.isLike()){
            isLike.setImageResource(R.mipmap.liked);
            isLike.setTag(R.mipmap.liked);
        }else {
            isLike.setImageResource(R.mipmap.like);
            isLike.setTag(R.mipmap.like);
        }
        isLike.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if((int)view.getTag()==R.mipmap.like){
                    ((ImageView) view).setImageResource(R.mipmap.liked);
                    ((ImageView) view).setTag(R.mipmap.liked);
                }else {
                    ((ImageView) view).setImageResource(R.mipmap.like);
                    ((ImageView) view).setTag(R.mipmap.like);
                }
            }
        });

        return view1;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }
}
